package com.hubery.designpattern.design.template.commodity;

import com.hubery.designpattern.enums.PrizeTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lixueyang
 * @since 2023/2/28 10:40
 */
@Slf4j
@Component
public class CommodityServiceSupport {

    @Resource
    private List<CommodityService> commodityServiceList;

    private Map<PrizeTypeEnum, CommodityService> commodityServiceMap = new EnumMap<>(PrizeTypeEnum.class);

    @PostConstruct
    private void init() {
        commodityServiceMap = commodityServiceList.stream().collect(Collectors.toMap(CommodityService::getPrizeTypeEnum, Function.identity()));
    }

    public CommodityService getCommodityServiceService(PrizeTypeEnum prizeTypeEnum) {
        return commodityServiceMap.getOrDefault(prizeTypeEnum, null);
    }
}
